import { apiGetBalanceList } from '../../api/index'
import { formatTime } from '../../common/js/timer'

let times
Page({
  /**
   * 页面的初始数据
   */
  data: {
    commSelect: 0,
    commTitleList: [
      { name: '全部', sta: 0 },
      { name: '已结算', sta: 2 },
      { name: '待结算', sta: 1 }
    ],
    params: {
      page_index: 1,
      page_size: 20,
      type: 2,
      status: 0
    },
    isMOre: false,
    comDetail: [],
    showTime: [],
    loads: true
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad (options) {
    this.setData({
      isIphoneX: getApp().globalData.isIphoneX,
      navHeight: getApp().globalData.navHeight * 2
    })
    this.getDetail()
  },
  timestampToTime (s, index) {
    let showTime = this.data.showTime
    if (s > 0) {
      times = setTimeout(() => {
        let day = Math.floor(s / 3600 / 24)
        let h = Math.floor(s / 3600 % 24)
        let min = Math.floor(s / 60 % 60)
        h = this.add(h)
        min = this.add(min)
        showTime[index] = day > 0 ? `${day}天${h}小时${min}分` : `${h}小时${min}分`
        this.setData({
          showTime
        })
        s -= 1
        if (s <= 0) {
          clearTimeout(times)
        } else {
          this.timestampToTime(s, index)
        }
      }, 1000)
    } else {
      showTime[index] = '0天'
      this.setData({
        showTime
      })
    }
  },
  add (m) {
    return m < 10 ? '0' + m : m
  },
  getDetail () {
    let params = this.data.params
    apiGetBalanceList(params).then(
      res => {
        console.log(res)
        for (let i = 0; i < res.data.data.length; i++) {
          // res.data.data[i].modity_time = formatTime(res.data.data[i].modity_time, 'Y-M-D h:m:s')
          res.data.data[i].create_time = formatTime(res.data.data[i].create_time, 'Y-M-D h:m:s')
          if (res.data.data[i].end_time === !NaN) {
            let createTime = res.data.data[i].end_time + res.data.data[i].create_time
            res.data.data[i].end_time = createTime - new Date().getTime() / 1000
            if (res.data.data[i].status === 0) {
              this.timestampToTime(res.data.data[i].end_time, i)
            }
          }
        }
        let isMOre = false
        let data = this.data.comDetail
        if (res.data.data.length >= 20) {
          isMOre = true
        }
        this.setData({
          comDetail: data.concat(res.data.data),
          isMOre,
          loads: false
        })
        wx.stopPullDownRefresh()
      }
    )
  },
  commSelects (e) {
    let params = this.data.params
    params['status'] = e.target.dataset.sta
    this.setData({
      comDetail: [],
      commSelect: e.target.dataset.index
    })
    this.getDetail()
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady () {

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload () {
    clearTimeout(times)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh () {
    this.data.params['page_index'] = 1
    this.setData({
      comDetail: []
    })
    this.getDetail()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom () {
    if (this.data.isMOre) {
      this.data.params['page_index']++
      this.getDetail()
    }
  }
})
